package com.io.controller;

import com.io.dto.ApiResult;
import com.io.utils.MinioUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;

/**
 * The class/interface 文件操作部分
 *
 * @author guod
 * @version 1.0 use jdk 1.8
 */
@Api(tags = "文件操作部分")
@RestController
@RequestMapping(value = "/minio")
public class MinoiController {

    @Autowired
    private MinioUtils minioUtils;

    @PostMapping(value = "/updateFileOne")
    public ApiResult updateFile(MultipartFile file) throws Exception {
        return minioUtils.uploadFile(file, "ydb");
    }

    @ApiOperation("浏览器预览文件（只能是PDF）")
    @GetMapping("/viewFile/{objectName}")
    public void preViewPicture(@PathVariable("objectName") String objectName, HttpServletResponse response) throws Exception {
        InputStream stream = minioUtils.getObject("ydb", objectName);
        minioUtils.viewFile(stream, response, "application/pdf");
    }

    @ApiOperation("下载文件")
    @GetMapping(value = "/downloadFile/{objectName}")
    public ApiResult downloadWord(@PathVariable("objectName") String objectName) {
        return ApiResult.ok();
    }
}
